package cn.project.biz;

import cn.project.dao.UserDao;
import cn.project.model.User;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Component("userBiz")
@Transactional(propagation = Propagation.SUPPORTS ,readOnly = true,rollbackFor = RuntimeException.class)
public class Userbiz {
    /*
    * 也是service
    * */
    @Resource(name ="userDao")
    private UserDao userDao;
    /*添加用户*/
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public void addUser(User user){
     userDao.addUser(user);
    }
    /*删除用户*/
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public void delUser(int userid){
        userDao.delUser(userid);
    }
    /*修改用户*/
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public void updateUser(User user){
        userDao.updateUser(user);
    }
    public User get(int userid){
        return userDao.get(userid);
    }

    public List searchUser(User user){
        return userDao.searchUser(user);
    }
    public List searchUser(String hql){
        return userDao.searchUser(hql);
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    /*一次添加多个用户*/
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public void addUsers(List<User> list){
        try {
            for (User user:list) {
                userDao.addUser(user);
            }
        }catch (Exception e){
            throw  new RuntimeException();
        }
    }
}
